package edu.xhxy;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class Main {
    public static void main(String[] args) throws Exception {
         create();
        putData();
    }

    public static void create() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "centos01, centos02, centos03");

        TableName tableName = TableName.valueOf("student");
        TableDescriptorBuilder tableDescBuilder = TableDescriptorBuilder.newBuilder(tableName);

        ColumnFamilyDescriptorBuilder familyDescBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("inf"));
        ColumnFamilyDescriptor familyDesc = familyDescBuilder.build();

        tableDescBuilder.setColumnFamily(familyDesc);

        TableDescriptor tableDesc = tableDescBuilder.build();

        try (Connection conn = ConnectionFactory.createConnection(conf)) {
            Admin admin = conn.getAdmin();
            admin.createTable(tableDesc);
        }
    }

    public static void putData() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "centos01, centos02, centos03");
        try (Connection conn = ConnectionFactory.createConnection(conf)) {

            TableName tableName = TableName.valueOf("student");
            Table table = conn.getTable(tableName);

            Put put1 = new Put(Bytes.toBytes("1001"));
            put1.addColumn(Bytes.toBytes("inf"), Bytes.toBytes("name"), Bytes.toBytes("zhangsan"));

            table.put(put1);

            table.close();
        }
    }
}